package com.geekstudy.week5;

import com.geekstudy.week5.entity.DemoEntity;
import com.geekstudy.week5.kimmking.api.ISchool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ImportResource;


@SpringBootApplication
public class Week5SpringBootMain {

    static final Logger logger = LoggerFactory.getLogger(Week5SpringBootMain.class);

    public static void main(String[] args) {
        ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(Week5SpringBootMain.class);
        logger.info("beanCount:{} ", configurableApplicationContext.getBeanDefinitionCount());

        // 第 2 题
        work2(configurableApplicationContext);

        // 第 8 题
        work8(configurableApplicationContext);
    }

    /**
     * 作业2 Spring Bean装配
     * @param configurableApplicationContext
     */
    public static void work2(ConfigurableApplicationContext configurableApplicationContext) {
        // 获取 XML配置
        DemoEntity demoEntityXml = configurableApplicationContext.getBean("demoEntityXml", DemoEntity.class);
        logger.info("demoEntityXml:{} ", demoEntityXml);
        DemoEntity demoEntityXmlInit = configurableApplicationContext.getBean("demoEntityXmlInit", DemoEntity.class);
        logger.info("demoEntityXmlInit:{}", demoEntityXmlInit);

        // 获取注解配置
        DemoEntity demoEntityBean = configurableApplicationContext.getBean("demoEntity@Bean", DemoEntity.class);
        logger.info("demoEntityBean:{}", demoEntityBean);
        DemoEntity demoEntityBeanInit = configurableApplicationContext.getBean("demoEntityBeanInit", DemoEntity.class);
        logger.info("demoEntityBeanInit:{}", demoEntityBeanInit);

    }


    public static void work8(ConfigurableApplicationContext configurableApplicationContext) {
        ISchool iSchool = configurableApplicationContext.getBean(ISchool.class);
        iSchool.ding();
        logger.info("finish invoke ISchool ding()");
    }
}
